/*     */ package com.yizhilu.os.image.controller;
/*     */ 
/*     */ import com.yizhilu.os.image.util.FileUploadUtil;
/*     */ import com.yizhilu.os.image.util.FileUtil;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.text.SimpleDateFormat;
/*     */ import java.util.Calendar;
/*     */ import javax.servlet.ServletContext;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import javax.servlet.http.HttpSession;
/*     */ import org.json.JSONObject;
/*     */ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
/*     */ import org.springframework.web.bind.annotation.RequestMapping;
/*     */ import org.springframework.web.bind.annotation.RequestParam;
/*     */ import org.springframework.web.bind.annotation.ResponseBody;
/*     */ import org.springframework.web.multipart.MultipartFile;
/*     */ 
/*     */ @Controller
/*     */ @RequestMapping({"/file"})
/*     */ public class FileController
/*     */ {
/*  29 */   private String pathSeparator = File.separator;
/*  30 */   private static Logger logger = LoggerFactory.getLogger(FileController.class);
/*     */ 
/*     */   @RequestMapping(value={"/upload"}, method={org.springframework.web.bind.annotation.RequestMethod.OPTIONS})
/*     */   public void uploads(HttpServletRequest request, HttpServletResponse response)
/*     */   {
/*  36 */     response.addHeader("Access-Control-Allow-Origin", "*");
/*  37 */     response.addHeader("Access-Control-Allow-Method", "POST");
/*  38 */     response.addHeader("Access-Control-Max-Age", "100");
/*  39 */     response.addHeader("Access-Control-Allow-Headers", "Content-Type");
/*  40 */     response.addHeader("Access-Control-Allow-Credentials", "false");
/*     */   }
/*     */ 
/*     */   @RequestMapping(value={"/upload"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
/*     */   @ResponseBody
/*     */   public JSONObject upload(HttpServletRequest request, @RequestParam("files") MultipartFile[] mtpfiles, HttpSession session, HttpServletResponse response)
/*     */   {
/*  51 */     response.addHeader("Access-Control-Allow-Origin", "*");
/*  52 */     response.addHeader("Access-Control-Allow-Methods", "POST");
/*  53 */     response.addHeader("Access-Control-Max-Age", "100");
/*  54 */     response.addHeader("Access-Control-Allow-Headers", "Content-Type");
/*  55 */     response.addHeader("Access-Control-Allow-Credentials", "false");
/*  56 */     JSONObject node = new JSONObject();
/*     */ 
/*  58 */     String webappPath = session.getServletContext().getRealPath("/");
/*     */ 
/*  60 */     String uploadDirPath = webappPath + "upload" + this.pathSeparator + getYearMonthDay();
/*  61 */     String[] paths = FileUtil.getSavePathByRequest(request);
/*     */ 
/*  63 */     File uploadDir = new File(paths[0]);
/*     */     try {
/*  65 */       String url = new FileUploadUtil(uploadDir, mtpfiles, paths[1]).upload(true);
/*  66 */       node.put("path", url);
/*  67 */       node.put("success", true);
/*  68 */       node.put("msg", "上传成功！");
/*     */     } catch (IllegalStateException e) {
/*  70 */       node.put("success", false);
/*  71 */       node.put("msg", "状态异常！");
/*     */     } catch (IOException e) {
/*  73 */       node.put("success", false);
/*  74 */       node.put("msg", "IO错误！");
/*     */     }
/*  76 */     return node;
/*     */   }
/*     */ 
/*     */   @RequestMapping({"/checkExists"})
/*     */   public void checkExists(@RequestParam String filename, HttpServletResponse response, HttpSession session)
/*     */   {
/*     */     try {
/*  83 */       response.addHeader("Access-Control-Allow-Origin", "*");
/*  84 */       response.addHeader("Access-Control-Allow-Methods", "*");
/*  85 */       response.addHeader("Access-Control-Max-Age", "100");
/*  86 */       response.addHeader("Access-Control-Allow-Headers", "Content-Type");
/*  87 */       response.addHeader("Access-Control-Allow-Credentials", "false");
/*  88 */       String webappPath = session.getServletContext().getRealPath("/");
/*     */ 
/*  90 */       String uploadDirPath = webappPath + "upload" + this.pathSeparator + getYearMonthDay();
/*  91 */       File file = new File(new File(uploadDirPath), filename);
/*  92 */       if (file.exists())
/*     */       {
/*  94 */         response.getWriter().write("1");
/*     */       }
/*     */       else
/*  97 */         response.getWriter().write("0");
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/*     */     }
/*     */   }
/*     */ 
/*     */   public String getYearMonthDay()
/*     */   {
/* 111 */     return new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
/*     */   }
/*     */ }

/* Location:           F:\work\公司\质量学院\ducumentQIN\项目更新\web\20171122\
 * Qualified Name:     com.yizhilu.os.image.controller.FileController
 * JD-Core Version:    0.6.1
 */